package net.xiezihao.product_service.service.impl;

import net.xiezihao.product_service.domain.Product;
import net.xiezihao.product_service.service.ProductService;
import net.xiezihao.product_service.service.UserServiceFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private UserServiceFeignClient client;
    private static final Map<Integer, Product> daoMap = new HashMap<>();
    @Value("${server.port}")
    private int port;


    static {
        Product p1 = new Product(1, "iphoneX", 9999, 10);
        Product p2 = new Product(2, "冰箱", 9769, 80);
        Product p3 = new Product(3, "洗衣机", 999, 30);
        Product p4 = new Product(4, "桌子", 999, 10);
        Product p5 = new Product(5, "椅子", 699, 100);
        Product p6 = new Product(6, "自慰棒", 499, 30);
        Product p7 = new Product(7, "机械键盘", 999, 10);
        daoMap.put(p1.getId(), p1);
        daoMap.put(p2.getId(), p2);
        daoMap.put(p3.getId(), p3);
        daoMap.put(p4.getId(), p4);
        daoMap.put(p5.getId(), p5);
        daoMap.put(p6.getId(), p6);
        daoMap.put(p7.getId(), p7);
    }


    @Override
    public List<Product> listProduct() {
        Collection<Product> collection = daoMap.values();
        ArrayList<Product> list = new ArrayList<>(collection);
        return list;
    }

    @Override
    public Product findById(int id) {
        Product product = daoMap.get(id);
        Product result = new Product();
        BeanUtils.copyProperties(product, result);
        result.setName(product.getName() + " from " + port);
        return result;
    }

    @Override
    public Map diaoyong() {
        String user = client.getUser();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("project", daoMap);
        map.put("user", user);
        return map;
    }


}
